
import { _decorator, Collider, Component, ITriggerEvent, Node, SystemEvent } from 'cc';
import { Constant } from '../framework/Constant';
import { PoolManager } from '../framework/PoolManager';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = Bullet
 * DateTime = Tue Feb 27 2024 13:26:41 GMT+0800 (中国标准时间)
 * Author = kun12303
 * FileBasename = Bullet.ts
 * FileBasenameNoExtension = Bullet
 * URL = db://assets/script/bullet/Bullet.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/en/
 *
 */


 
@ccclass('Bullet')
export class Bullet extends Component {
    
    @property
    private _bulletSpeed = 0;
    private _direction = Constant.Direction.MIDDLE;
    private _isEnemyBullter = false;

    protected onEnable(): void {
        const collider = this.getComponent(Collider)
        collider.on('onTriggerEnter',this._onTriggerEnter,this)
    }

    protected onDisable(): void {
        const collider = this.getComponent(Collider)
        collider.off('onTriggerEnter',this._onTriggerEnter,this)
    }

    update (deltaTime: number) {
        if(SystemEvent.EventType.TOUCH_START){
            const pos = this.node.position;
            let moveLength = 0;
            if(this._isEnemyBullter){
                moveLength = pos.z + this._bulletSpeed;
                this.node.setPosition(pos.x, pos.y, moveLength)
                if(moveLength>50){
                    PoolManager.instance().putNode(this.node);
                    // this.node.destroy();
                }
            }else{
                moveLength = pos.z - this._bulletSpeed;
                if(this._direction===Constant.Direction.LEFT){
                    this.node.setPosition(pos.x - this._bulletSpeed*0.2, pos.y, moveLength)
                }else if(this._direction===Constant.Direction.RIGHT){
                    this.node.setPosition(pos.x + this._bulletSpeed*0.2, pos.y, moveLength)
                }else{
                    this.node.setPosition(pos.x, pos.y, moveLength)
                }
                
                if(moveLength<-50){
                    PoolManager.instance().putNode(this.node);
                    // this.node.destroy();
                }
            }
            
            
            
        }
        
    }

    _onTriggerEnter(event:ITriggerEvent){
        PoolManager.instance().putNode(this.node);
        // this.node.destroy()
    }

    show(speed:number, isEnemyBullter:boolean,direction=Constant.Direction.MIDDLE){
        this._bulletSpeed = speed
        this._isEnemyBullter = isEnemyBullter
        this._direction = direction
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.3/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.3/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.3/manual/en/scripting/life-cycle-callbacks.html
 */
